<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="500px"
    @close="close"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="昵称" prop="userName">
        <el-input v-model.trim="form.userName" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="姓名" prop="realName">
        <el-input v-model.trim="form.realName" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input v-model.trim="form.phone" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="身份证" prop="card">
        <el-input v-model.trim="form.card" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="gender">
        <el-input v-model.trim="form.gender" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="年龄" prop="age">
        <el-input v-model.trim="form.age" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model.trim="form.password" autocomplete="off"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { doEdit, doAdd } from '@/api/userList'

  export default {
    name: 'TableEdit',
    data() {
      return {
        form: {
          realName: '',
          phone: '',
          card: '',
          gender: '',
          age: '',
        },
        rules: {
          realName: [
            { required: true, trigger: 'blur', message: '请输入姓名' },
          ],
          phone: [{ required: true, trigger: 'blur', message: '请输入手机号' }],
          card: [{ required: true, trigger: 'blur', message: '请输入身份证' }],
          gender: [{ required: true, trigger: 'blur', message: '请输入性别' }],
          age: [{ required: true, trigger: 'blur', message: '请输入年龄' }],
        },
        title: '',
        dialogFormVisible: false,
      }
    },
    created() {},
    methods: {
      showEdit(row) {
        if (!row) {
          this.title = '添加'
        } else {
          this.title = '编辑'
          this.form = Object.assign({}, row)
        }
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
        this.$emit('fetchdata')
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            if (this.title == '编辑') {
              const { msg } = await doEdit(this.form)
              this.$baseMessage(msg, 'success')
              this.$refs['form'].resetFields()
              this.dialogFormVisible = false
              // this.$parent.fetchData()
              // console.log(this.$parent)
              this.$emit('save')
              this.form = this.$options.data().form
            } else {
              const { msg } = await doAdd(this.form)
              this.$baseMessage(msg, 'success')
              this.$refs['form'].resetFields()
              this.dialogFormVisible = false
              this.$parent.fetchData()
              this.form = this.$options.data().form
            }
          } else {
            return false
          }
        })
      },
    },
  }
</script>
